{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "65102716-6a98-4251-9bbc-bd341062e221",
   "metadata": {},
   "source": [
    "## GraphQL Agent Tool\n",
    "\n",
    "This example walks through two examples of connecting an Agent to a GraphQL server, one unauthenticated endpoint and one authenticated. To start, we initialize the OpenAI package with our api key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c6cb675-121b-4f47-b91d-7fcd6e746fd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup OpenAI Agent\n",
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-your-key\"\n",
    "\n",
    "from llama_index.core.agent.workflow import FunctionAgent\n",
    "from llama_index.llms.openai import OpenAI"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b39e01e-23ce-4b35-87f9-040bd7f693d0",
   "metadata": {},
   "source": [
    "## Unauthenticated server\n",
    "\n",
    "Our first example is connecting to a server provided by Apollo as an introduction to GraphQL. It provides some data about SpaceX rockets and launches.\n",
    "\n",
    "To get started, we setup the url we want to query and some basic headers, then we ask the agent to execute a query against the server."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0992299-3f8e-4122-abb7-54e6ed06511b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.tools.graphql.base import GraphQLToolSpec\n",
    "\n",
    "# Unauthenticated example against a public server\n",
    "url = \"https://spacex-production.up.railway.app/\"\n",
    "headers = {\n",
    "    \"content-type\": \"application/json\",\n",
    "}\n",
    "\n",
    "graphql_spec = GraphQLToolSpec(url=url, headers=headers)\n",
    "agent = FunctionAgent(\n",
    "    tools=graphql_spec.to_tool_list(),\n",
    "    llm=OpenAI(model=\"gpt-4.1\"),\n",
    ")\n",
    "\n",
    "print(await agent.run(\"get the id, name and type of the Ships from the graphql endpoint\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbd6a409-472b-4136-912b-2112210c7daf",
   "metadata": {},
   "source": [
    "The Agent was able to form the GraphQL based on our instructions, and additionally provided some extra parsing and formatting for the data. Nice!\n",
    "\n",
    "## Authenticated Server\n",
    "\n",
    "The next example shows setting up authentication headers to hit a private server, representing a Shopify store that has opened up GraphQL access based on an admin API token. To get started with an example similar to this, see the shopify.ipynb notebook. You will also find a more detailed example of using the Schema Definition Language file to fully unlock the GraphQL API."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98e45bed-c0ae-40ba-8504-904e56da502a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Authenticated example against a Shopify store\n",
    "\n",
    "url = \"https://your-store.myshopify.com/admin/api/2023-07/graphql.json\"\n",
    "headers = {\n",
    "    \"accept-language\": \"en-US,en;q=0.9\",\n",
    "    \"content-type\": \"application/json\",\n",
    "    \"X-Shopify-Access-Token\": \"your-admin-key\",\n",
    "}\n",
    "\n",
    "graphql_spec = GraphQLToolSpec(url=url, headers=headers)\n",
    "agent = FunctionAgent(\n",
    "    tools=graphql_spec.to_tool_list(),\n",
    "    llm=OpenAI(model=\"gpt-4.1\"),\n",
    ")\n",
    "\n",
    "print(\n",
    "    await agent.run(\"get the id and title of the first 3 products from the graphql server\")\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
